<?php

    function dumpvar($var){
        echo '<pre>';
        if(is_array($var)){
            print_r($var);
        } else {
            echo $var;
        }
        echo '</pre>';
        die();
    }

    function phone_display($phonestring, $cach = ' '){
        if($cach == null){
            $cach = ' ';
        }

        $arr1 = substr($phonestring, -3, 3);
        $arr2 = substr($phonestring, -6, 3);
        $arr3 = substr($phonestring, 0, strlen($phonestring) - 6);

        return $arr3.$cach.$arr2.$cach.$arr1;
    }

    function date_display(){
        return 'Ngày '.date('d').' tháng '.date('m').' năm '.date('Y');
    }

    function initalize_post_var(){
        $func_num_args = func_num_args();
        $func_post_vars = func_get_args();

        if($func_num_args > 0){
            foreach ($func_post_vars as $post_var) {
                if(!isset($_POST[$post_var])){
                    $_POST[$post_var] = '';
                }
            }
        }
    }

    function sanitize($data){
        return mysql_real_escape_string($data);
    }


	function nvs_createDate($ngay,$format)
	{
		if($format == "dd-mm-yyyy")
		{
			$arr = array();
			$arr = explode('-',$ngay);
			return mktime(0,0,0,$arr[1],$arr[0],$arr[2]);
		}
		if($format == "mm-dd-yyyy")
		{
			$arr = array();
			$arr = explode('-',$ngay);
			return mktime(0,0,0,$arr[0],$arr[1],$arr[2]);
		}
		if($format == "dd-mm-yyyy h:i")
		{
			$arr2 = explode(' ',$ngay);
			$arr = explode('-',$arr2[0]);
			$arr3 = explode(':',$arr2[1]);

			return mktime($arr3[0],$arr3[1],0,$arr[1],$arr[0],$arr[2]);
		}
		if($format == "mm-dd-yyyy h:i")
		{
			$arr2 = explode(' ',$ngay);
			$arr = explode('-',$arr2[0]);
			$arr3 = explode(':',$arr2[1]);

			return mktime($arr3[0],$arr3[1],0,$arr[0],$arr[1],$arr[2]);
		}

	}
	function nvs_getIP()
	{
		if(!empty($_SERVER['HTTP_CLIENT_IP']))
		{
			$ip=$_SERVER['HTTP_CLIENT_IP'];
		}
		elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
		{
			$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
		}
		else
		{
			$ip=$_SERVER['REMOTE_ADDR'];
		}
		return $ip;
	}
	// Lay ngay theo dinh dang
	function nvs_getDate($ngay,$format)
	{
		return date($format,$ngay);
	}
	function getId($value)
	{
		$url = explode("-",$value);
		$id = $url[count($url)-1];
		return $id;
	}
	function delAlias($value)
	{
		return str_replace("-","",$value);
	}
	function issetor(&$variable, $or = NULL) {
		return empty($variable) ? $or : $variable;
	}
	function delVietnameses($value)
	{
		$marTViet=array("à","á","ạ","ả","ã","â","ầ","ấ","ậ","ẩ","ẫ","ă",
		"ằ","ắ","ặ","ẳ","ẵ","è","é","ẹ","ẻ","ẽ","ê","ề"
		,"ế","ệ","ể","ễ",
		"ì","í","ị","ỉ","ĩ",
		"ò","ó","ọ","ỏ","õ","ô","ồ","ố","ộ","ổ","ỗ","ơ"
		,"ờ","ớ","ợ","ở","ỡ",
		"ù","ú","ụ","ủ","ũ","ư","ừ","ứ","ự","ử","ữ",
		"ỳ","ý","ỵ","ỷ","ỹ",
		"đ",
		"À","Á","Ạ","Ả","Ã","Â","Ầ","Ấ","Ậ","Ẩ","Ẫ","Ă"
		,"Ằ","Ắ","Ặ","Ẳ","Ẵ",
		"È","É","Ẹ","Ẻ","Ẽ","Ê","Ề","Ế","Ệ","Ể","Ễ",
		"Ì","Í","Ị","Ỉ","Ĩ",
		"Ò","Ó","Ọ","Ỏ","Õ","Ô","Ồ","Ố","Ộ","Ổ","Ỗ","Ơ"
		,"Ờ","Ớ","Ợ","Ở","Ỡ",
		"Ù","Ú","Ụ","Ủ","Ũ","Ư","Ừ","Ứ","Ự","Ử","Ữ",
		"Ỳ","Ý","Ỵ","Ỷ","Ỹ",
		"Đ");

		$marKoDau=array("a","a","a","a","a","a","a","a","a","a","a"
		,"a","a","a","a","a","a",
		"e","e","e","e","e","e","e","e","e","e","e",
		"i","i","i","i","i",
		"o","o","o","o","o","o","o","o","o","o","o","o"
		,"o","o","o","o","o",
		"u","u","u","u","u","u","u","u","u","u","u",
		"y","y","y","y","y",
		"d",
		"A","A","A","A","A","A","A","A","A","A","A","A"
		,"A","A","A","A","A",
		"E","E","E","E","E","E","E","E","E","E","E",
		"I","I","I","I","I",
		"O","O","O","O","O","O","O","O","O","O","O","O"
		,"O","O","O","O","O",
		"U","U","U","U","U","U","U","U","U","U","U",
		"Y","Y","Y","Y","Y",
		"D");
		return str_replace($marTViet,$marKoDau,$value);
	}

	function setId($value,$id)
	{
		$value = delVietnameses($value);
		$value = str_replace(array(":","\"",",","-","."),"",$value);
		$value = str_replace(array(" "),"-",$value);
		$value = str_replace('"',"-",$value);
		return $value."-".$id;
	}
	function getAlias($value)
	{
		$value = delVietnameses($value);
		$value = str_replace(array(":","\"",",","-","."),"",$value);
		$value = str_replace(" ","-",$value);
		return $value;
	}
	function currentDomain()
	{
		$protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https')
                === FALSE ? 'http' : 'https';
		$host     = $_SERVER['HTTP_HOST'];
		$script   = $_SERVER['SCRIPT_NAME'];
		$params   = $_SERVER['QUERY_STRING'];

		$currentUrl = $protocol . '://' . $host . $script . '?' . $params;

		return $currentUrl;
	}
	function cutString($value,$n)
	{
		$temp = explode(" ",$value);
		if(count($temp) > $n)
		{
			for($i=0;$i<$n;$i++)
			{
				$temp2[] = $temp[$i];
			}
			return implode(' ',$temp2)." ...";
		}
		return implode(' ',$temp);
	}

	function nvs_Xtem($view,$theme,$module="")
	{
		return ($module != "") ? $xtm = new XTemplate($view.".tpl",LT2N_THEME_PATH.$theme."/modules/".$module."/") : $xtm = new XTemplate($view.".tpl",LT2N_THEME_PATH.$theme."/");
	}
	function nvs_XtemBlock($view,$theme)
	{
		return $xtm = new XTemplate($view.".tpl",LT2N_THEME_PATH.$theme."/blocks/");
	}
	function nvs_getImages($content,$image_position,$default_image, $out_element_tag = TRUE)
	{
		$matched = array();
		$pattern = '/<img.+src=\"([^\"]+)\"[^\/>]+[\/]?>/i';
		preg_match_all($pattern,$content,$matched);
		if($out_element_tag)
		{
			if(count($matched) > $image_position)
				return $matched[0][$image_position];
			return '<img src="'.$default_image.'" />';
		}
		if(count($matched) > $image_position)
			return  $matched[1][$image_position];
		return $default_image;
	}
?>